package com.quick.service.impl;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpSession;

import com.quick.config.QuickSystem;
import com.quick.config.TokenStorage;
import com.quick.util.Convert;
import com.quick.util.JSONUtil;
import com.quick.util.QuickTool;
import com.quick.util.TokenUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quick.login.doamin.dto.UserLoginDTO;
import com.quick.login.doamin.pojo.UserLogin;
import com.quick.login.doamin.vo.QuickUserVo;
import com.quick.mapper.UserLoginMapper;
import com.quick.service.IUserLoginService;
import org.springframework.util.DigestUtils;

@Service
public class UserLoginServiceImpl implements IUserLoginService {
	
	@Autowired
	private UserLoginMapper userLoginMapper;
	
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;
	
	
	@Override
	public String login(UserLoginDTO login) {
		String token = TokenUtil.generateKey();
		checkLogin(login);
		QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<UserLogin>();
		queryWrapper.lambda().eq(UserLogin::getUserName, login.getUserName());
		UserLogin  user = userLoginMapper.selectOne(queryWrapper);
		QuickTool.isNull(user, "用户不存在");
		String pwd = DigestUtils.md5DigestAsHex(login.getPassword().getBytes());
		Assert.isTrue(user.getPassword().equalsIgnoreCase(pwd),"用户密码不正确");
		Integer iveInterval = 1000*60;
		if(QuickTool.getStoreType() == TokenStorage.LOCAL) {
			HttpSession session =  QuickTool.getRequest().getSession();
			session.setAttribute(QuickTool.getTokenKey(), user);
			session.setMaxInactiveInterval(iveInterval);
		}else {
			String res = JSONUtil.toStr(user);
			String newToken = QuickSystem.getKey(token);
			redisTemplate.opsForValue().set(newToken, res,iveInterval, TimeUnit.SECONDS);
		}
		return token;
	}

	@Override
	public Boolean register(UserLoginDTO register) {
		UserLogin login = Convert.convert(register,UserLogin.class);
		checkUserExist(login.getUserName());
		String pwd = DigestUtils.md5DigestAsHex(login.getPassword().getBytes());
		login.setPassword(pwd);
		return userLoginMapper.insert(login)>0;
	}

	@Override
	public void checkUserExist(String userName) {
		QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<UserLogin>();
		queryWrapper.lambda().eq(UserLogin::getUserName, userName);
		UserLogin  user = userLoginMapper.selectOne(queryWrapper);
		Assert.isNull(user,"用户已存在");
	}

	@Override
	public QuickUserVo getUser() {
		if(QuickTool.getStoreType() == TokenStorage.LOCAL) {
			HttpSession session =  QuickTool.getRequest().getSession();
			System.out.println(session);
		}else {
			String tokenKey = QuickTool.getTokenKey();
			String token = QuickTool.getRequest().getHeader(tokenKey);
			String uuid = QuickSystem.getKey(token);
			Object res = redisTemplate.opsForValue().get(uuid);
			UserLogin original = JSONUtil.toBean(res.toString(), UserLogin.class);
			QuickUserVo quick = new QuickUserVo();
			quick.setUserName(original.getUserName());
			return quick;
		}
		return new QuickUserVo();
	}
}
